local zhongbukeyan = fk.CreateSkill{
  name = "lzsj__zhongbukeyan",
  tags = { Skill.Wake },
}

Fk:loadTranslationTable{
  ["lzsj__zhongbukeyan"] = "忠不可言",
  [":lzsj__zhongbukeyan"] = "觉醒技，一轮结束时，若场上身份数等于角色数，你可令一名其他角色进行一个额外回合；此回合结束时，若此回合被造成的伤害至少为0/1/2点，你增加等量体力上限，获得“享乐”/删去“敌不可剿”括号内容/获得<a href='liu_mark'>“流”标记</a>。",
  ["#lzsj__zhongbukeyan-choose"] = "忠不可言：你可令一名其他角色进行一个额外回合",
  ['liu_mark'] = "结束阶段将武将牌翻面",
  ["@@lzsj__zhongbukeyan"] = "额外回合",
  ["@@zhongbukeyan_over"] = "<font color=red><b>忠！诚！</b></font>",
  ["@@shencai_liu"] = "流",
  [":@@shencai_liu"] = "结束阶段将武将牌翻面",

  ["$lzsj__zhongbukeyan1"] = "华清池水彻骨寒，兵谏书成烫手铁！",
  ["$lzsj__zhongbukeyan2"] = "五间厅枪声落，从此忠奸皆哑谜！",
}

local checkUniqueRoles = function(room)
  local identity_count = { lord = 0, loyalist = 0, rebel = 0, renegade = 0, civilian = 0 }--身份出现次数
  for _, p in ipairs(room.alive_players) do
      local role = p.role
      if role == "rebel_chief" then role = "rebel" end
      if identity_count[role] then
          identity_count[role] = identity_count[role] + 1
      else
          -- 处理未知身份（如扩展包角色）
          return false
      end
  end
  -- 校验主公唯一性
  if identity_count.lord ~= 1 then return false end
  -- 校验其他身份不重复
  for role, count in pairs(identity_count) do
      if role ~= "lord" and count > 1 then
          return false
      end
  end
  return true
end

zhongbukeyan:addEffect(fk.RoundEnd, {
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(zhongbukeyan.name) and player:usedSkillTimes(zhongbukeyan.name, Player.HistoryGame) == 0
  end,
  can_wake = function(self, event, target, player, data)
    return checkUniqueRoles(player.room)
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local tos = room:askToChoosePlayers(player, {
      targets = room:getOtherPlayers(player),
      min_num = 1,
      max_num = 1,
      prompt = "#lzsj__zhongbukeyan-choose",
      skill_name = zhongbukeyan.name,
      cancelable = false,
    })
    if #tos > 0 then
      tos[1]:gainAnExtraTurn(true, zhongbukeyan.name, nil, {
        zhongbukeyan_source = player,
      })
    end
  end,
})

zhongbukeyan:addEffect(fk.TurnEnd, {
  is_delay_effect = true,
  mute = true,
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(zhongbukeyan.name) and data.reason == zhongbukeyan.name and
      data.extra_data and data.extra_data.zhongbukeyan_source and not data.extra_data.zhongbukeyan_source.dead
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local n = 0
    room.logic:getActualDamageEvents(1, function (e)
      if e.data.damage > 0 then
        n = n + e.data.damage
      end
    end, Player.HistoryTurn)
    if not player.dead then
      room:handleAddLoseSkills(player, "lzsj__xiangle", nil, true, false)
      if n >= 1 then
        room:changeMaxHp(player, n)
        room:addPlayerMark(player, "@@lzsj__dibukejiao")
        if n >= 2 then
          room:changeKingdom(player, "west", true)
          room:addPlayerMark(player, "@@shencai_liu")
        end
      end
    end
  end,
})

zhongbukeyan:addEffect(fk.EventPhaseStart, {
  anim_type = "negative",
  is_delay_effect = true,
  mute = true,
  can_trigger = function (self, event, target, player, data)
    return target == player and player:getMark("@@shencai_liu") > 0 and player.phase == Player.Finish and not player.dead
  end,
  on_use = function (self, event, target, player, data)
    player:turnOver()
  end,
})

return zhongbukeyan

